package cn.huaqingcheng.tianshu.common.core;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.DurationSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.YearMonthSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.YearSerializer;

import java.io.Serial;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.YearMonth;

import static java.time.format.DateTimeFormatter.ofPattern;

/**
 * 时间序列化
 */
public class JavaTimeModule extends SimpleModule {

    public static final String YEAR = "yyyy";

    public static final String YEAR_MONTH = "yyyy-MM";

    public static final String TIME = "HH:mm:ss";

    public static final String DATE = "yyyy-MM-dd";

    public static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 指定序列化规则
     */
    public JavaTimeModule() {
        super(PackageVersion.VERSION);

        addDeserializer(Year.class, new YearDeserializer(ofPattern(YEAR)));
        addDeserializer(YearMonth.class, new YearMonthDeserializer(ofPattern(YEAR_MONTH)));
        addDeserializer(LocalTime.class, new LocalTimeDeserializer(ofPattern(TIME)));
        addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(DATE)));
        addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(ofPattern(DATE_TIME)));
        addDeserializer(Duration.class, DurationDeserializer.INSTANCE);
        addDeserializer(Instant.class, InstantDeserializer.INSTANT);

        // 时间反序列化规则
        addSerializer(Year.class, new YearSerializer(ofPattern(YEAR)));
        addSerializer(YearMonth.class, new YearMonthSerializer(ofPattern(YEAR_MONTH)));
        addSerializer(LocalTime.class, new LocalTimeSerializer(ofPattern(TIME)));
        addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(DATE)));
        addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(ofPattern(DATE_TIME)));
        addSerializer(Duration.class, DurationSerializer.INSTANCE);
        addSerializer(Instant.class, InstantSerializer.INSTANCE);
    }

}
